package com.example.administrator.democ;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements InOnDelListener{
    private ListView lvSco;
    private Button btnAdd;
    private ArrayList<Score> scoreArrayList = new ArrayList<>();
    private ScoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvSco = findViewById(R.id.lv_sco);
        btnAdd = findViewById(R.id.btn_add);

        initAdd();

        adapter = new ScoAdapter(MainActivity.this, scoreArrayList,MainActivity.this);
        lvSco.setAdapter(adapter);

        initLvStu();

        readFromDB();
    }

    //从数据库中读取数据
    private void readFromDB() {
        Uri uri = Uri.parse("content://com.hjn.xn/score");

        //每次查询时，先清空
        scoreArrayList.clear();

        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if(cursor != null && cursor.getCount() > 0){
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                String no = cursor.getString(0);
                int android = cursor.getInt(1);
                int english = cursor.getInt(2);

                Score s = new Score(no, android, english);
                scoreArrayList.add(s);
            }
            cursor.close();
        }

        adapter.notifyDataSetChanged();
    }

    //编辑学生
    private void initLvStu() {
        lvSco.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                Score student = scoreArrayList.get(i);
                intent.putExtra("Score", student);
                startActivityForResult(intent, 1002);
            }
        });
    }

    //添加学生
    private void initAdd() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode == 2001){
            Score score = (Score) data.getSerializableExtra("SCO");
            addStuToDB(score);
        }else if(resultCode == 3001){
            Score score = (Score) data.getSerializableExtra("SCO");
            updateStuToDB(score);
        }
    }

    //在数据库中修改学生
    private void updateStuToDB(Score score) {
        Uri uri = Uri.parse("content://com.hjn.xn/score");
        ContentValues values = new ContentValues();
        values.put("stuno", score.getStuNo());
        values.put("android", score.getAndroid());
        values.put("english", score.getEnglish());
        String where = "stuno = '"+score.getStuNo()+"'";
        getContentResolver().update(uri, values, where, null);
        readFromDB();
    }

    //将学生添加到数据库中
    private void addStuToDB(Score score) {
        Uri uri = Uri.parse("content://com.hjn.xn/score");
        ContentValues values = new ContentValues();
        values.put("stuno", score.getStuNo());
        values.put("android", score.getAndroid());
        values.put("english", score.getEnglish());
        getContentResolver().insert(uri, values);

        readFromDB();
    }

    //删除学生
    @Override
    public void del(Score score) {
        Uri uri = Uri.parse("content://com.hjn.xn/score");
        String where = "stuno = '"+score.getStuNo()+"'";
        getContentResolver().delete(uri, where, null);
        readFromDB();
    }
}
